#include "stdafx.h"
#include "FlowButton.h"

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if (p) { delete (p); (p) = 0; } }
#endif // SAFE_DELETE

IMPLEMENT_DUICONTROL(CFlowButtonUI)
/////////////////////////////////////////////////////////////////////////////////////
//
//

CFlowButtonUI::CFlowButtonUI()
	: CButtonUI(),
	m_bIsOnce(false)
{
	m_nFramePosition = 0;
	m_nFrameCount = 0;
	m_nForeAnimaTimerDuration = 60;

}

CFlowButtonUI::~CFlowButtonUI()
{
}

//void CFlowButtonUI::SetAnimationImages(vector<DuiLib::CDuiString> vecList)
//{
//	vecAnimation = vecList;
//	m_nFrameCount = vecList.size();
//}


void CFlowButtonUI::SetBkTextureImage(LPCTSTR pStrImage)
{
	m_sBkTextureImage = pStrImage;
	Invalidate();
}

LPCTSTR CFlowButtonUI::GetBkTextureImage()
{
	return m_sBkTextureImage;
}

void CFlowButtonUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if (_tcsicmp(pstrName, _T("bktexture")) == 0) 
		SetBkTextureImage(pstrValue);
	else CButtonUI::SetAttribute(pstrName, pstrValue);
}

bool CFlowButtonUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
{
	__super::DoPaint(hDC, rcPaint, pStopControl);

	for (vector<LINESTRUCT>::iterator iter = m_tLines.begin(); iter != m_tLines.end(); iter++) {
		PaintLine(hDC, *iter);
	}

	for (vector<Gdiplus::Rect>::iterator iter = m_tTextureLines.begin(); iter != m_tTextureLines.end(); iter++) {
		PaintWithTexture(hDC, *iter);
	}

	return true;
}

void CFlowButtonUI::DoEvent(TEventUI& event)
{
	CButtonUI::DoEvent(event);

	if (event.Type == UIEVENT_TIMER)
	{
		if ((UINT_PTR)event.wParam == EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID)
			OnTimer((UINT_PTR)event.wParam);
	}
}

void CFlowButtonUI::OnTimer(UINT_PTR idEvent)
{
	if (idEvent != EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID)
		return;

	//DrawForeFrame();
	
	this->Invalidate();
}

void CFlowButtonUI::DoInit()
{
	m_pTextureBrush_bk = new Gdiplus::TextureBrush(CRenderEngine::GdiplusLoadImage(GetBkTextureImage()));
	//m_pTextureBrush_bk->SetTransform(
	//	&Gdiplus::Matrix(
	//		75.0 / 640.0, 0.0f, 0.0f,
	//		75.0 / 480.0, 0.0f, 0.0f
	//	)
	//);
}

void CFlowButtonUI::ToPlay()
{
	//m_bIsOnce = false;
	//m_pManager->SetTimer(this, EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID, m_nForeAnimaTimerDuration);
	//LINESTRUCT line = { 0, 0, 200, 100 };
	//AddLine(line);
	Gdiplus::Rect line = { 0, 150, 150, 250 };
	AddTextureLine(line);
}

void CFlowButtonUI::ToPlayOnce()
{
	m_bIsOnce = true;
	m_pManager->SetTimer(this, EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID, m_nForeAnimaTimerDuration);
}

void CFlowButtonUI::ToStop()
{
	m_pManager->KillTimer(this, EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID);
}

void CFlowButtonUI::AddLine(LINESTRUCT line)
{
	m_tLines.push_back(line);
	PaintLine(line);
}

void CFlowButtonUI::AddTextureLine(Gdiplus::Rect line)
{
	m_tTextureLines.push_back(line);
	PaintWithTexture(line);
}

void CFlowButtonUI::PaintLineBk(HDC hDC)
{
	Gdiplus::Graphics graphics(hDC);
	Gdiplus::Pen      pen(Gdiplus::Color(255, 0, 0, 255), 20);
	graphics.DrawLine(&pen, 0, 0, 200, 100);
}

void CFlowButtonUI::PaintLineBk()
{
	PAINTSTRUCT ps = { 0 };
	::BeginPaint(m_pManager->GetPaintWindow(), &ps);
	Gdiplus::Graphics graphics(m_pManager->GetPaintDC());
	Gdiplus::Pen      pen(Gdiplus::Color(255, 0, 0, 255), 20);
	graphics.DrawLine(&pen, 0, 0, 200, 100);
	::EndPaint(m_pManager->GetPaintWindow(), &ps);
}

void CFlowButtonUI::PaintLine(HDC hDC, LINESTRUCT line)
{
	Gdiplus::Graphics graphics(hDC);
	Gdiplus::Pen      pen(Gdiplus::Color(255, 0, 0, 255), 20);
	graphics.DrawLine(&pen, line.start.x, line.start.y, line.end.x, line.end.y);
}

void CFlowButtonUI::PaintLine(LINESTRUCT line)
{
	PAINTSTRUCT ps = { 0 };
	::BeginPaint(m_pManager->GetPaintWindow(), &ps);

	PaintLine(m_pManager->GetPaintDC(), line);

	::EndPaint(m_pManager->GetPaintWindow(), &ps);
}

void CFlowButtonUI::PaintWithTexture(HDC hDC, Gdiplus::Rect rect)
{
	Gdiplus::Graphics graphics(hDC);
	
	graphics.FillRectangle(m_pTextureBrush_bk, rect);
}

void CFlowButtonUI::PaintWithTexture(Gdiplus::Rect rect)
{
	PAINTSTRUCT ps = { 0 };
	::BeginPaint(m_pManager->GetPaintWindow(), &ps);

	PaintWithTexture(m_pManager->GetPaintDC(), rect);

	::EndPaint(m_pManager->GetPaintWindow(), &ps);
}

//void CFlowButtonUI::DrawForeFrame()
//{
//	m_pManager->KillTimer(this, EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID);
//	if (m_nFramePosition >= m_nFrameCount) {
//		m_nFramePosition = 0;
//		if (m_bIsOnce) {
//			return;
//		}
//		else {
//			goto _goon;
//		}
//	}
//	else {
//		_goon:
//		SetForeImage((LPCTSTR)vecAnimation[m_nFramePosition]);
//		m_nFramePosition++;
//		m_pManager->SetTimer(this, EVENT_ANIMA_FLOW_FORE_BITMAP_TIMERID, m_nForeAnimaTimerDuration);
//	}
//}





